/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   04.05.2010
 * @version SVN: $Id: ProductNotExistForm.java 155 2010-05-19 13:51:47Z f.aarndt@web.de $
 */

package org.cakis.client.forms;

import java.rmi.RemoteException;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import org.cakis.client.CakisMIDlet;
import org.cakis.client.commands.AddNewProductCommand;
import org.cakis.client.commands.ResumeCommand;
import org.cakis.webservice.ProductQuantity;
import org.cakis.webservice.StockUnit;

/**
 * ProductNotExistForm
 *
 * @author Florian Arndt
 */
public class ProductNotExistForm extends AbstractForm {
	private TextField productName;
	private ChoiceGroup stockUnits;
	private TextField productQuantity;
	private StockUnit[] units;
	private CakisMIDlet midlet;
	private String productResult;
	private ProductQuantity quantity;
	
	public ProductNotExistForm(CakisMIDlet midlet,
			String productResult, boolean offlineScannedBarcodes) {
		super("Eingabefenster", midlet);
		this.midlet = midlet;
		this.productResult = productResult;
			
		StringItem info = new StringItem(
				"Produkt nicht gefunden. Bitte eingeben!", "");
		StringItem ean = new StringItem("EAN: ", productResult);
		productName = new TextField("Produktname eingeben:",
				"", 256, TextField.ANY);
		stockUnits = new ChoiceGroup("Select StockUnit", Choice.EXCLUSIVE);
		getStockUnits();
		 
		productQuantity = new TextField(
				"Standardmenge eingeben:", "1", 256, TextField.ANY);

		this.append(info);
		this.append(ean);
		this.append(productName);
		this.append(productQuantity);
		this.append(stockUnits);
		this.addCommand(new AddNewProductCommand(midlet, this, offlineScannedBarcodes));
		this.addCommand(new ResumeCommand(midlet));
		midlet.display(this);
	}

	/**
	 * Get all StockUnits from the server.
	 */
	private void getStockUnits() {
		try {
			units = midlet.getService().getAllStockUnits();
			
			for (int i = 0; i < units.length; i++) {
				stockUnits.append(units[i].getName(), null);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}		 
	}
	
	public String getProductName() {
		return productName.getString();
	}
	
	public ProductQuantity getProductQuantity() {
		quantity = new ProductQuantity();
		quantity.setQuantity(Float.valueOf(productQuantity.getString()));
		quantity.setUnit(units[stockUnits.getSelectedIndex()]);
		return quantity;
	}

	public String getProductResult() {
		return productResult;
	}
}
